package mantel;

import static org.junit.Assert.*;
import java.io.*;

import java.util.ArrayList;
import java.util.Random;

import jsc.datastructures.PairedData;

import org.junit.Before;
import org.junit.Test;

import tool.Utility;

public class mantelTestTest {

	double[][] X;
	double[][] Y;
	int size = 100;
	int permutations = 1000;
	boolean standardise = false;
	Random r;
	int FUNC =2;
	
	double[] testArray = {0.1,0.1,0.2,0.2,0.2,0.3,0.5,0.55,0.6,0.7};
	int groupSize =5;
	
	
	@Before
	public void setUp() throws Exception {
		X = new double[size][size];
		Y = new double[size][size];
		
		r = new Random();
		
		for (int i = 1; i < size; i++) {
			for (int j = 0; j < i; j++) {
				X[i][j] = Math.abs(i-j-1);
				double f = func(X[i][j],FUNC);
				Y[i][j] = f;
				X[j][i] = Math.abs(i-j-1);
				Y[j][i] = f;
			}
		}
		//Utility.printMat(X);System.out.println();
		//Utility.printMat(Y);
	}

	@Test
	public void testmantel(){
		double[] a = Utility.distMatrixToVector(X);
		double[] b = Utility.distMatrixToVector(Y);
		//PairedData p = new PairedData(a,b);
		//System.out.println(p);
		
		double[] s = mantelTest.mantel(X, Y, permutations, standardise);
		//System.out.println("\n"+"Mantel: "+s[0]+" Sig: "+s[1]+"\n");
	}
	
	@Test
	public void testSegmented_mantel_cor() throws IOException {
		Utility.printMat(X);System.out.println();
		//Utility.printMat(Y);
		double[] Xv = Utility.distMatrixToVector(X);
		double[] Yv = Utility.distMatrixToVector(Y);
		PrintWriter p = new PrintWriter(new FileWriter(new File("xOut.dat")));
		for (int i = 0; i < Yv.length; i++) {
			p.println(Xv[i]+" "+Yv[i]);
		}
		p.close();
		
		
		mantelTest.printCorrelog(mantelTest.segmented_mantel_cor(X, Y, 1000, groupSize));
	}
	
	@Test
	public void testgetBreakPoints(){
		ArrayList<Double> bPoints = mantelTest.getBreakPoints(Y, 3);
		//System.out.println(bPoints);
	}
	
	
	private double func(double x,int FUNC_TYPE){
		switch(FUNC_TYPE){
			case 0:
				return x+r.nextGaussian();
			
			default:
				return x+r.nextGaussian();
				
			case 1:
				return -(x+r.nextGaussian());
				
			case 2:
				return x+10*Math.sin(Math.PI*x/10) + 5*r.nextGaussian();
				
		}
		
	}

}
